package fr.ups.jim.isi.tp4;

import java.awt.Color;
import java.awt.Point;
import java.awt.geom.Line2D;

public class LineItem extends CanvasItem{

	LineItem (PersistentCanvas canvas, Color o, Color f, Point p) {
		super(canvas, o, f);
		shape = new Line2D.Double(p.x, p.y, p.x, p.y);
	}
	
	LineItem (LineItem originItem) {
		super(originItem.canvas, originItem.outlineColor, originItem.fillColor);
		Line2D.Double originLine = (Line2D.Double)originItem.shape;
		shape = new Line2D.Double(originLine.x1, originLine.y1,
								originLine.x2, originLine.y2);
	
	}
	
	// this method overrides the method in its parent class
	// because line is to difficult to select, so here we use some approximate
	// values
	public boolean contains(Point p) {
		Line2D.Double l = (Line2D.Double)shape;
		if (l.ptSegDist(p) < 5) 
			return true;
		else 
			return false;
	}
	
	public void update(Point p) {
		((Line2D.Double)shape).x2 = p.x;
		((Line2D.Double)shape).y2 = p.y;
		canvas.repaint();
		
	}


	public CanvasItem duplicate() {
		CanvasItem newLine = new LineItem(this);
		newLine.move(10, 10);
		canvas.addItem(newLine);
		return newLine;
	}
	
	
	public void move(int disX, int disY) {
		Line2D.Double line = (Line2D.Double)shape;
		line.x1 += disX;
		line.x2 += disX;
		line.y1 += disY;
		line.y2 += disY;
		canvas.repaint();
	}

}
